package com.zhentao.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.StudentMapper;
import com.zhentao.mapper.ClassInstructorTeacherMapper;
import com.zhentao.pojo.Student;
import com.zhentao.pojo.ClassInstructorTeacher;
import com.zhentao.service.StudentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author hp
* @description 针对表【student】的数据库操作Service实现
* @createDate 2025-07-09 15:28:47
*/
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
    implements StudentService{

    @Autowired
    private ClassInstructorTeacherMapper classInstructorTeacherMapper;

    @Override
    public List<Student> getStudentsByTeacherId(Integer teacherId) {
        // 1. 查找教师所带班级ID
        List<ClassInstructorTeacher> relations = classInstructorTeacherMapper.selectList(
            new QueryWrapper<ClassInstructorTeacher>().eq("teacher_id", teacherId));
        List<Integer> classIds = relations.stream()
            .map(ClassInstructorTeacher::getClassId)
            .collect(Collectors.toList());
        if (classIds.isEmpty()) return new java.util.ArrayList<>();
        // 2. 查找这些班级下的所有学生
        return this.baseMapper.selectList(new QueryWrapper<Student>().in("class_id", classIds));
    }
}




